import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 139. 单词拆分
 * @author: WXY
 * @create: 2023-01-09 18:56
 * @Version 1.0
 **/
public class Num139_wordBreak {
    public static boolean wordBreak(String s, List<String> wordDict) {
        int sLen = s.length();
        boolean[] dp = new boolean[sLen + 1];
        //dp[i] 表示字符串s前i个字符组成的组成的字符是否能够被wordDice中的单词表示。
        dp[0] = true;
        for (int i = 1; i <= sLen; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[sLen];
    }

    public static void main(String[] args) {
        String s = "catsanddog";
        String[] str = {"cats", "dog", "sand", "and", "cat"};
        List<String> list = new ArrayList<>();
        for (String string : str) {
            list.add(string);
        }
        boolean b = wordBreak(s, list);
        System.out.println(b);
    }
}
